
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;
import com.huizhan.web.view.*;

/**
 * 证件记录api
 */
 
@Api(tags = "DocumentRecord",description = "证件记录")
@RestController
public class DocumentRecordController {
    
    @Autowired
    private DocumentRecordDao dao;
   
    /**
     * 获取证件记录集合
     * @return api结果
     */
    @ApiOperation(value = "获取证件记录集合")
    @RequestMapping(value =  "/api/DocumentRecord/",method = {RequestMethod.GET})
    public ApiResult<List<DocumentRecord>> Get(){
         ApiResult<List<DocumentRecord>> result=new ApiResult<List<DocumentRecord>>();
        try 
        {
            List<DocumentRecord> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 查询证件记录集合
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询证件记录集合")
    @RequestMapping(value =  "/api/DocumentRecord/Search",method = {RequestMethod.POST})
    public ApiResult<List<DocumentRecord>> Search(@RequestBody PageDocumentRecord model){
         ApiResult<List<DocumentRecord>> result=new ApiResult<List<DocumentRecord>>();
         
        try
        {    Page<DocumentRecord> page;
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"document_record_Id") ;
            if(model.getType().equals(1)){
                page = dao.findByWhere1(model.getName(),model.getPhone(),model.getProjectId(),model.getDocumentTypeId(),pageable);
            }else{
                 page = dao.findByWhere(model.getName(),model.getPhone(),model.getProjectId(),model.getDocumentTypeId(),pageable);
            } 
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        
        return result;
    }

    /**
     * 根据ID获取证件记录信息
     * @param id 证件记录ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取证件记录信息")
    @RequestMapping(value =  "/api/DocumentRecord/{id}",method = {RequestMethod.GET})
    public ApiResult<DocumentRecord> Get(@PathVariable Long id){
        ApiResult<DocumentRecord> result=new ApiResult<DocumentRecord>();
        try
        {
            DocumentRecord model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改证件记录信息
     * @param id 证件记录ID
     * @param model 证件记录信息
     * @return api结果
     */
    @ApiOperation(value = "修改证件记录信息")
    @RequestMapping(value =  "/api/DocumentRecord/{id}",method = {RequestMethod.PUT})
    public ApiResult<DocumentRecord> Edit(@PathVariable Long id,@RequestBody DocumentRecord model){
        ApiResult<DocumentRecord> result=new ApiResult<DocumentRecord>();
        try
        {
            DocumentRecord record=dao.getOne(id);

            if(record!=null){
                record.setNumber(model.getNumber());
                dao.save(record);
                result.setData(model);
            }else{
                result.setHasError(true);
                result.setMessage("参数错误");
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加证件记录信息
     * @param model 证件记录信息
     * @return api结果
     */
    @ApiOperation(value = "添加证件记录信息")
    @RequestMapping(value =  "/api/DocumentRecord/",method = {RequestMethod.POST})
    public ApiResult<DocumentRecord> Add(@RequestBody DocumentRecord model){
        ApiResult<DocumentRecord> result=new ApiResult<DocumentRecord>();
        try
        {
            model.setCreateDate(new Date());
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除证件记录信息
     * @param id 证件记录ID
     * @return api结果
     */
    @ApiOperation(value = "删除证件记录信息")
    @RequestMapping(value =  "/api/DocumentRecord/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

